"use strict";

var KTUtil = function () {

  var breakpoints = {
    sm: 544,
    // Small screen / phone
    md: 768,
    // Medium screen / tablet
    lg: 992,
    // Large screen / desktop
    xl: 1200 // Extra large screen / wide desktop

  };

  return {
    
    isMobileDevice: function isMobileDevice() {
      return this.getViewPort().width < this.getBreakpoint('lg') ? true : false;
    },

    isDesktopDevice: function isDesktopDevice() {
      return KTUtil.isMobileDevice() ? false : true;
    },

    getViewPort: function getViewPort() {
      var e = window,
          a = 'inner';

      if (!('innerWidth' in window)) {
        a = 'client';
        e = document.documentElement || document.body;
      }

      return {
        width: e[a + 'Width'],
        height: e[a + 'Height']
      };
    },

    /**
     * Checks whether given device mode is currently activated.
     * @param {string} mode Responsive mode name(e.g: desktop,
     *     desktop-and-tablet, tablet, tablet-and-mobile, mobile)
     * @returns {boolean}
     */
    isInResponsiveRange: function isInResponsiveRange(mode) {
      var breakpoint = this.getViewPort().width;

      if (mode == 'general') {
        return true;
      } else if (mode == 'desktop' && breakpoint >= this.getBreakpoint('lg') + 1) {
        return true;
      } else if (mode == 'tablet' && breakpoint >= this.getBreakpoint('md') + 1 && breakpoint < this.getBreakpoint('lg')) {
        return true;
      } else if (mode == 'mobile' && breakpoint <= this.getBreakpoint('md')) {
        return true;
      } else if (mode == 'desktop-and-tablet' && breakpoint >= this.getBreakpoint('md') + 1) {
        return true;
      } else if (mode == 'tablet-and-mobile' && breakpoint <= this.getBreakpoint('lg')) {
        return true;
      } else if (mode == 'minimal-desktop-and-below' && breakpoint <= this.getBreakpoint('xl')) {
        return true;
      }

      return false;
    },

    /**
    * Checks whether given device mode is currently activated.
    */
    isBreakpointUp: function isBreakpointUp(mode) {
      var width = this.getViewPort().width;
      var breakpoint = this.getBreakpoint(mode);
      return width >= breakpoint;
    },
    isBreakpointDown: function isBreakpointDown(mode) {
      var width = this.getViewPort().width;
      var breakpoint = this.getBreakpoint(mode);
      return width < breakpoint;
    },

    /**
     * Generates unique ID for give prefix.
     * @param {string} prefix Prefix for generated ID
     * @returns {boolean}
     */
    getUniqueID: function getUniqueID(prefix) {
      return prefix + Math.floor(Math.random() * new Date().getTime());
    },

    /**
     * Gets window width for give breakpoint mode.
     * @param {string} mode Responsive mode name(e.g: xl, lg, md, sm)
     * @returns {number}
     */
    getBreakpoint: function getBreakpoint(mode) {
      return breakpoints[mode];
    },

    /**
     * Simulates delay
     */
    sleep: function sleep(milliseconds) {
      var start = new Date().getTime();

      for (var i = 0; i < 1e7; i++) {
        if (new Date().getTime() - start > milliseconds) {
          break;
        }
      }
    },

    /**
     * Gets randomly generated integer value within given min and max range
     * @param {number} min Range start value
     * @param {number} max Range end value
     * @returns {number}
     */
    getRandomInt: function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    },

    isArray: function isArray(obj) {
      return obj && Array.isArray(obj);
    },
    isEmpty: function isEmpty(obj) {
      for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
          return false;
        }
      }

      return true;
    },
    numberString: function numberString(nStr) {
      nStr += '';
      var x = nStr.split('.');
      var x1 = x[0];
      var x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;

      while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
      }

      return x1 + x2;
    },
    colorDarken: function colorDarken(color, amount) {
      var subtractLight = function subtractLight(color, amount) {
        var cc = parseInt(color, 16) - amount;
        var c = cc < 0 ? 0 : cc;
        c = c.toString(16).length > 1 ? c.toString(16) : "0".concat(c.toString(16));
        return c;
      };

      color = color.indexOf("#") >= 0 ? color.substring(1, color.length) : color;
      amount = parseInt(255 * amount / 100);
      return color = "#".concat(subtractLight(color.substring(0, 2), amount)).concat(subtractLight(color.substring(2, 4), amount)).concat(subtractLight(color.substring(4, 6), amount));
    },
    colorLighten: function colorLighten(color, amount) {
      var addLight = function addLight(color, amount) {
        var cc = parseInt(color, 16) + amount;
        var c = cc > 255 ? 255 : cc;
        c = c.toString(16).length > 1 ? c.toString(16) : "0".concat(c.toString(16));
        return c;
      };

      color = color.indexOf("#") >= 0 ? color.substring(1, color.length) : color;
      amount = parseInt(255 * amount / 100);
      return color = "#".concat(addLight(color.substring(0, 2), amount)).concat(addLight(color.substring(2, 4), amount)).concat(addLight(color.substring(4, 6), amount));
    },

    jsonToCamelCase : function jsonToCamelCase(data, alterFunc){
      var that = this;
      function convertToCamelCase(target) {
          if(!target){
            return;
          }
          const keys = Object.keys(target);
          keys.forEach(key => {
              if (typeof target[key] === 'object')
                  convertToCamelCase(target[key]);
      
              const converted = that.toCamelCase(key);
              if(!alterFunc){
                if (key !== converted) {
                  target[converted] = target[key];
                  delete target[key];
                }
              }else{
                target[converted] = alterFunc(converted,target[key]);
                if (key !== converted) {
                  delete target[key];
                }
              }
          });
      }
      convertToCamelCase(data);
      return data;
    },

    toCamelCase: function toCamelCase(str){
       return str.charAt(0).toLowerCase() + str.slice(1);
    }
  };
}();